$(function () {
    var usernamecheck = true;
    var passwordcheck = true;

    $(".err").each(function () {
        showERR($(this));
    });
    $(".input_class").focus(function () { // 得到焦点清除错误信息
        var id = ($(this).attr("id"));
        ERRID = id + "ERR";
        var ele = $("#" + ERRID)
        ele.text("");
        showERR(ele);
    });

    $("#username").blur(function () { // 用户名输入框失去焦点校验
        var value = $("#username").val();
        usernamecheck = true;
        if ($(this).val() == null || $(this).val() == "") {
            // 非空校验
            $("#usernameERR").text("用户名不能为空");
            check = false;
        }
        showERR($("#usernameERR"));
    })
    $("#password").blur(function () { // 密码框失去焦点校验
        var password = $(this).val();
        passwordcheck = true;
        if (password == null || password == "") {
            $("#passwordERR").text("密码不能为空");
            passwordcheck = false;
        }
        showERR($("#passwordERR"));
    })
    $("#submit").click(function () {
        if ($("#password").val() == null || $("#password").val() == "" || $("#username").val() == null || $("#username").val() == null) {
            alert("用户名或密码不能为空")
            return false;
        }
        if ($("#verifiedcode").val() == null || $("#verifiedcode").val() == null) {
            alert("验证码不能为空");
            return false;
        }
        var username = $("#username").val();
        var password = $("#password").val();
        var verifiedcode = $("#verifiedcode").val();

        $.ajax({
            type: "POST",
            url: "/blog/users/login",
            data: {
                "username": username,
                "password": password,
                "verifiedcode": verifiedcode
            },
            dataType: "JSON",
            success: function (response) {
                if (response.state == 200) {
                    setTimeout(function () {
                        window.location.href = "mainframe.html";
                    }, 2000)
                } else {
                    $("#verifiedcode").val("");
                }
                alert(response.message);
                $("#verifiedcodeimg").attr("src",
                    "/blog/public/getverifiedcode?" + Math.random())
            }
        });
    })


})


function showERR(ele) {
    var text = ele.text();
    if (!text) {
        ele.hide();
    } else {
        ele.show();
    }
}